package com.lanou.bookstore.order.web.servlet;

import com.lanou.bookstore.cart.domain.Cart;
import com.lanou.bookstore.cart.domain.CartItem;
import com.lanou.bookstore.order.domain.Order;
import com.lanou.bookstore.order.domain.OrderItem;
import com.lanou.bookstore.order.service.OrderService;
import com.lanou.bookstore.user.domain.User;
import com.lanou.bookstore.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet(name = "OrderServlet", urlPatterns = "/order.do")
public class OrderServlet extends BaseServlet {
    OrderService orderService = new OrderService();

    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        Order order = new Order();
        Date date=new Date();
        SimpleDateFormat simpleDateFormat =
                new SimpleDateFormat("yyyy-MM-dd HH:mm");
        order.setOid(UUID.randomUUID().toString().replace("-", ""));
        order.setState(1);
        order.setOrderTime(simpleDateFormat.format(date));
        order.setUid(user.getUid());
        order.setTotal(cart.getTotal());


        List<OrderItem> orderItemList = new ArrayList<>();
        for (CartItem cartItem : cart.getCartItems()) {
            OrderItem oi = new OrderItem();
            oi.setIid(UUID.randomUUID().toString().replace("-", ""));
            oi.setCount(cartItem.getCount());
            oi.setBook(cartItem.getBook());
            oi.setSubtotal(cartItem.getSubtotal());
            oi.setOid(order.getOid());
            oi.setBid(cartItem.getBook().getBid());

            orderItemList.add(oi);
        }
        order.setOrderItemList(orderItemList);
        orderService.add(order);
        request.setAttribute("order", order);
        cart.clear();
        request.getRequestDispatcher("/jsps/order/desc.jsp").forward(request, response);

    }

    private void orders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect(request.getContextPath() + "/jsps/user/login.jsp");
            return;
        }
        List<Order> orders = orderService.findByUid(user.getUid());
        request.setAttribute("orders", orders);
        request.getRequestDispatcher("/jsps/order/list.jsp")
                .forward(request, response);

    }

    private void load(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String oid = request.getParameter("oid");
        //点击付款,按oid查询该订单
        Order order = orderService.load(oid);
        request.setAttribute("order", order);
        request.getRequestDispatcher("/jsps/order/desc.jsp")
                .forward(request, response);

    }

    private void confirm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String oid = request.getParameter("oid");
        String msg = orderService.confirm(oid);
        request.setAttribute("msg", msg);
        request.getRequestDispatcher("/jsps/order/msg.jsp")
                .forward(request, response);
    }


}